package cjlu.ea.h.controller;


import cjlu.ea.h.common.AuthUtil;
import cjlu.ea.h.common.R;
import cjlu.ea.h.entity.ByszgshbLsb;
import cjlu.ea.h.entity.OpTime;
import cjlu.ea.h.service.OpTimeService;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Update;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-09-11
 */
@RestController
@RequestMapping("/h/op-time")
@CrossOrigin
@Slf4j
public class OpTimeController {
    @Autowired
    OpTimeService opTimeService;
    @Autowired
    RedisTemplate redisTemplate;

    @GetMapping("/getAllOpTime")
    public R getAllOpTime(){
        return R.ok().data("list",opTimeService.list(null));
    }

    @PostMapping("/getConditionOpTime")
    public R getConditionOpTime(@RequestBody OpTime opTime){
        List<OpTime> list = opTimeService.getConditionLiost(opTime);
        return R.ok().data("list",list);
    }

    @DeleteMapping("/deleteById/{id}")
    public R deleteById(@PathVariable Integer id, HttpServletRequest request){
        AuthUtil authUtil = new AuthUtil();
        authUtil.setRedisTemplate(redisTemplate);
        String role = authUtil.getAuthentications(request);
        if("admin".equals(role)){
            log.info("admin 可操作...");
            boolean result = opTimeService.removeById(id);
            if (result){
                return R.ok().message("删除成功");
            }else{
                return R.error().message("删除失败");
            }
        }else{
            log.error("普通用户无权限操作 可操作时间表....");
            return R.error().message("无权限操作");
        }


    }

    @GetMapping("/getById/{id}")
    public R getById(@PathVariable Integer id){
        return R.ok().data("time",opTimeService.getById(id));
    }

    @PostMapping("/updateById")
    public R updateById(@RequestBody OpTime opTime){
        opTimeService.updateById(opTime);
        return R.ok();
    }

    @PostMapping("save")
    public R save(@RequestBody OpTime opTime){
        opTimeService.save(opTime);
        return R.ok();
    }

}

